import cv2
import mediapipe as mp
import math

mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands

#打开摄像头
cap = cv2.VideoCapture(0)
#获取摄像头内置参数
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))#宽度
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))#高度

#矩形方块参数
square_x = int(width / 2)#矩形方块起始位置
square_y = int(height / 2)
square_width = 60 #矩形方块的宽  
square_height = 60 #矩形方块的高


l1 = 0
l2 = 0
on_square = False

hands =  mp_hands.Hands(
    model_complexity=0,
    min_detection_confidence=0.5,
    min_tracking_confidence=0.5)

while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
      print("Ignoring empty camera frame.")
    # To improve performance, optionally mark the image as not writeable to
    # pass by reference.
    frame.flags.writeable = False

    #对摄像头获得的图片进行处理
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    frame = cv2.flip(frame, 1)

    results = hands.process(frame) #返回关节坐标原点

    # Draw the hand annotations on the image.
    frame.flags.writeable = True
    frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
    #print(results.multi_hand_landmarks)
    if results.multi_hand_landmarks:#判断是否出现手
        for hand_landmarks in results.multi_hand_landmarks:#解析遍历每一双手
        #绘制关节关键点
          mp_drawing.draw_landmarks(
            frame,
            hand_landmarks,
            mp_hands.HAND_CONNECTIONS,
            mp_drawing_styles.get_default_hand_landmarks_style(),
            mp_drawing_styles.get_default_hand_connections_style())
        #保存21个关键点坐标
        x_list = []
        y_list = []
        for landmark in hand_landmarks.landmark:
            x_list.append(landmark.x)
            y_list.append(landmark.y)
        
        #获取食指指尖坐标
        index_figure_x = int(x_list[8] * width)
        index_figure_y = int(y_list[8] * height)
        #获取中指指尖坐标
        middle_figure_x = int(x_list[12] * width)
        middle_figure_y = int(y_list[12] * height)
        #判断食指是否在矩形方框内
        if(index_figure_x > square_x) and (index_figure_x < square_x + square_width) and (index_figure_y > square_y) and (index_figure_y < square_y + square_height):
            #print('in')#在矩形方块上
            if on_square == False:
                 on_square = True
                 l1 = abs(index_figure_x - square_x)
                 l2 = abs(index_figure_y - square_y)
            else:
                pass
        if on_square:
            square_x = index_figure_x - l1
            square_y = index_figure_y - l2
        figure_len = int(math.hypot(abs(int(middle_figure_x - index_figure_x)),abs(int(middle_figure_y - index_figure_y))))
        #取消移动矩形方块
        if on_square and figure_len < 40:
            on_square = False
            
    #绘制矩形方块
    cv2.rectangle(frame,(square_x,square_y),(square_x+square_width,square_y+square_height),(255,0,0),-1)
    
    # Flip the image horizontally for a selfie-view display.
    cv2.imshow('MediaPipe Hands', frame)
    if cv2.waitKey(5) & 0xFF == 27:
      break
cap.release()
cv2.destroyAllWindows()